import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.NLineInputFormat;

//import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

public class Uploads {
	private static String users = "";

	public static class Map extends MapReduceBase implements
			Mapper<LongWritable, Text, Text, Text> {

		private Text word = new Text();

		@Override
		public void map(LongWritable key, Text value,
				OutputCollector<Text, Text> output, Reporter reporter)
				throws IOException {
			try {
				String[] line = value.toString().split(",");
				if (line[2].equals("1")) {
					word.set(line[0]);
					output.collect(word, new Text(line[1]));
				} else {
					
				}
			} catch (Exception e) {

			}

		}
	}

	public static class Reduce extends MapReduceBase implements
			Reducer<Text, Text, Text, Text> {

		/**
		 * O reduce recebe a chave e o valor do map e os direciona de forma que
		 * a contagem não seja inclusa no resultado e a ordenação é feita pelo
		 * próprio map, a saída do reduce vai para o arquivo part-00000
		 */
		public void reduce(Text key, Iterator<Text> values,
				OutputCollector<Text, Text> output, Reporter reporter)
				throws IOException {
			try {
				while(values.hasNext()) {
					output.collect(key, values.next());
				}
//				
//				
//				
//				StringTokenizer tokenizer = new StringTokenizer(users, "\t");
//				while (tokenizer.hasMoreTokens()) {
//					String token = tokenizer.nextToken();
//					if (key.toString().split(",")[2].equals("1")) {
//						if (key.toString().contains(token.toString())) {
//							output.collect(new Text(token), new Text(key
//									.toString().split(",")[1]));
//						}
//					}
//
//				}
			} catch (ArrayIndexOutOfBoundsException e) {

			}

		}
	}

	public static void main(String[] args) throws IOException {
		JobConf conf = new JobConf(Uploads.class);
		conf.setJobName("Uploads");

		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(Text.class);

		conf.setMapperClass(Map.class);
		// conf.setCombinerClass(Reduce.class);
		conf.setReducerClass(Reduce.class);

		conf.setInputFormat(TextInputFormat.class);

		conf.setOutputFormat(TextOutputFormat.class);

		FileInputFormat.setInputPaths(conf, new Path(args[0]));
		FileOutputFormat.setOutputPath(conf, new Path(args[1]));

		JobClient.runJob(conf);
	}
}
